// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

export enum keyType {
    emptyKey = 0,
    redKey  =  1 ,
    blueKey =  2,
    greenKey = 3,
    orangeKey = 4,
    purpleKey = 5,
    whiteKey = 6
}

export enum keyUsing {
    boxKey  = 0,
    normal = 1,
    show = 2,
    press = 3,
    usingMoreScene = 4
}

//宝箱用途钥匙图片
let boxKeyPic =  ["dj-x03c","dj-x04c","dj-x01c","dj-x02c","dj-x05c","dj-x07c"];

@ccclass
export default class keyMediator extends cc.Component {

    @property({
        type : cc.Enum(keyType),

        displayName : "钥匙类型"
    })
    curKey : keyType = keyType.redKey;

    @property({
        type : cc.Enum(keyUsing),
        displayName : "钥匙类型"
    })
    keyPurpose : keyUsing = keyUsing.normal;

    onLoad(){
        if(this.keyPurpose == keyUsing.boxKey || this.keyPurpose == keyUsing.show){
            this.setBoxSkin(boxKeyPic[this.curKey - 1]);
        }

        if(this.keyPurpose == keyUsing.press){
            this.node.once("touchstart",this.touchStart,this);
        }
    }

    touchStart(event){
        this.node.opacity = 255;
        this.node.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
        this.node.getComponent(cc.PhysicsBoxCollider).sensor = false;
        this.node.getComponent(cc.PhysicsBoxCollider).apply();
    }

    setBoxSkin(url){
        cc.loader.loadRes("img/level/" + url,cc.SpriteFrame,(error, resource) => {
            if(error){
                console.log(error);
                return;
            }
            this.node.getComponent(cc.Sprite).spriteFrame = resource;
        })
    }



}
